//
//  PaperErrorsViewController.swift
//  Exam-iOS
//
//  Created by lishengfeng on 2018/3/18.
//  Copyright © 2018年 李胜锋. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa
import RxDataSources
import SnapKit
import Then

class PaperErrorsViewController: ViewController, Refreshable, LeftBarButtonItemable, NullData {

    var vm = PaperErrorsViewModel()
    
    var tableView: UITableView!
    var dataSource: RxTableViewSectionedReloadDataSource<PaperErrorsSection>!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        initUI()
        bindUI()
        tableView.mj_header.beginRefreshing()
    }
    
    private func initUI() {
        title = "错题"
        view.backgroundColor = UIColor.lsf_color16(0xF9F9F9)
        setLeftItem()
        
        tableView = UITableView.init(frame: CGRect.zero, style: .plain).then({ (tv) in
            tv.tableFooterView = UIView()
            tv.separatorStyle = .none
            tv.backgroundColor = UIColor.lsf_color16(0xF9F9F9)
            tv.register(PaperErrorsCell.classForCoder(), forCellReuseIdentifier: "PaperErrorsCell")
            
            view.addSubview(tv)
            tv.snp.makeConstraints({ (maker) in
                maker.edges.equalTo(safeAreaInsets())
            })
        })
    }
    
    private func bindUI() {
        dataSource = RxTableViewSectionedReloadDataSource.init(configureCell: { (ds, tv, ip, item) -> UITableViewCell in
            let cell = tv.dequeueReusableCell(withIdentifier: "PaperErrorsCell", for: ip) as! PaperErrorsCell
            let title = item.paperTitle
            let count = item.wrongItemTotal
            cell.set(title: title, count: count)
            return cell
        })
        
        
        tableView.rx.itemSelected.subscribe(onNext: { [weak self] (indexPath) in
            self?.tableView.deselectRow(at: indexPath, animated: true)
        }).disposed(by: disposeBag)
        
        tableView.rx.modelSelected(PaperErrorsModel.self).subscribe(onNext: { [weak self] (model) in
            guard let id = model.paperId else { return }
            guard let pUserSummaryId = model.pUserSummaryId else {return }
            let title = model.paperTitle
            let paperVC = PaperViewController.startPaper(id, status: .errorItems, title: title, pUserSummaryId: pUserSummaryId)
            self?.navigationController?.pushViewController(paperVC, animated: true)
        }).disposed(by: disposeBag)
        
        vm.vmDatas.asObservable().bind(to: tableView.rx.items(dataSource: dataSource)).disposed(by: disposeBag)
        initRefreshHeader(tableView) { [weak self] in
            self?.vm.requestCommand.onNext(true)
        }
        vm.autoSetRefreshStatus(header: tableView.mj_header, footer: nil).disposed(by: disposeBag)
        
        ///空数据处理
        vm.isNull.asObservable().subscribe(onNext: { [weak self] (isNull) in
            if isNull {
                let nilView = self?.nilView(text: "还没有错题记录哟~")
                self?.tableView.backgroundView = nilView
            }
            else {
                self?.tableView.backgroundView = nil
            }
        }).disposed(by: disposeBag)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
